<?php if( ! defined('BASEPATH')) exit('No direct script access allowed');
/********************
 *@param function:OplusCms 後臺系統管理的語言管理
 *@param datetime 2012-01-07
 *@param author solgh
 ********************/
class Languages extends Admins_Controller {
  
  private $data=array();
  private $language_id;
  public function __construct(){
    parent::__construct();
    $this->data['currentPage']='systems';
    $this->load->model('languages_mdl');
  }

  public function index(){
    $this->get_list();
    $this->data['defalut_language']=$this->settings['default_language'];
    $this->load->view('admins/languages_list_view',$this->data);
  }
  
  //獲取語言列表
  private function get_list(){
    $page = $this->input->get('p',TRUE);
    $page = (!empty($page) && is_numeric($page)) ? intval($page) : 1;
    $limit = $this->settings['default_admin_limit'];
    $offset = ($page - 1) * $limit;
    if($offset < 0){
      redirect('admins/languages');
    }
    $url_query=$_SERVER['QUERY_STRING'];
    if(!empty($url_query)){
      $url_query=preg_replace('/&p=(\d+)/','',$url_query);
    }
    $this->data['field']=$this->input->get('field');
    $this->data['sort']=$this->input->get('sort');
    $this->data['field']=!empty($this->data['field'])?$this->data['field']:'sort_order';
    $this->data['sort']=!empty($this->data['sort'])?$this->data['sort']:'asc';
    
    $data=array('status'=>1,'limits'=>$limit,'offset'=>$offset);
  
    $this->data['lists_count']=$this->languages_mdl->totle_languages($data);
    
    $data=!empty($this->data['field'])?array_merge($data,array('field'=>$this->data['field'])):$data;
    $data=!empty($this->data['sort'])?array_merge($data,array('sort'=>strtoupper($this->data['sort']))):$data;    
    
    $pagination = '';
    if($this->data['lists_count'] > $limit)
    {
      $this->dpagination->currentPage($page);
      $this->dpagination->items($this->data['lists_count']);
      $this->dpagination->limit($limit);
      $this->dpagination->adjacents(5);
      $this->dpagination->target(site_url('admins/languages'));
      $this->dpagination->parameterName('p');
      $this->dpagination->nextLabel('下一頁');
      $this->dpagination->PrevLabel('上一頁');
      $pagination = $this->dpagination->getOutput();
    }
    $this->data['pagination'] = $pagination;
    $this->data['lists']=$this->languages_mdl->get_languages($data);
  }
  
  //添加語言      
  public function add(){
    $this->load->view('admins/languages_form_view',$this->data);
  }
  //修改語言
  public function edit($edit_id=0){
    if(!empty($edit_id)){
      $this->data['edit_id']=$edit_id;
      $query=$this->languages_mdl->get_languages_by_id($edit_id);
      $this->data['name']=$query['name'];
      $this->data['filename']=$query['filename'];
      $this->data['code']=$query['code'];
      $this->data['status']=$query['status'];
      $this->data['sort_order']=$query['sort_order'];
      $this->load->view('admins/languages_form_view',$this->data);
    }else{
      show_error('對不起,參數出錯');
      exit;
    }
  }
  //執行添加或修改
  public function modify(){
    $edit_id=$this->input->post('edit_id');
    $this->language_id=$edit_id;
    $this->_load_validation_rules(); //執行驗證
    if ($this->form_validation->run() == FALSE){

      $this->data['edit_id']=$edit_id;
      $this->data['error']=true;
      $this->load->view('admins/languages_form_view',$this->data);
    
    }else{
      $data=array(
          'name'=>$this->input->post('name'),
          'filename'=>$this->input->post('filename'),
          'code'=>$this->input->post('code'),
          'sort_order'=>$this->input->post('sort_order'),
          'status'=>$this->input->post('status')
      );
      if(!empty($edit_id)){
        $operator_title='修改語言設定->'.$this->input->post('name');
        $action='修改';
        $result=$this->languages_mdl->edit_languages($data,$edit_id);
      }else{
        $operator_title='新增語言設定->'.$this->input->post('name');
        $action='新增';
        $result=$this->languages_mdl->add_languages($data);
      }
      $this->operator_log($operator_title,$action,$result);
      $msg = $result > 0?'恭喜,您的操作成功!':'對不起,您的操作失敗';
      $notify = $result > 0?'success':'error';
      $result>0?$this->db->cache_delete('admins', 'languages'):'';
      $this->nsession->set_flashdata($notify, $msg);
      redirect("admins/languages");
    }
        
  }
  //驗證數據格式
  private function _load_validation_rules(){
      $this->form_validation->set_rules('name','語言','trim|required|htmlspecialchars|max_length[20]');
      $this->form_validation->set_rules('filename','文件夾','trim|required|is_unique[clickrcms_language.filename.id.'.$this->language_id.']|max_length[20]');
      $this->form_validation->set_rules('code','語言代碼','trim|max_length[20]');
      $this->form_validation->set_rules('sort_order','排序','trim|numeric|max_length[3]');
      $this->form_validation->set_rules('status','狀態','trim|required|numeric|max_length[1]');
  }
  //刪除語言
  public function delete($delete_id=''){
   if(!empty($delete_id)){
      $result=$this->languages_mdl->delete_languages($delete_id);
      $this->operator_log('刪除語言設定->ID:'.$delete_id,'刪除',$result);
      $msg = $result > 0?'恭喜,您的操作成功!':'對不起,您的操作失敗';
      $notify = $result > 0?'success':'error';
      $this->nsession->set_flashdata($notify, $msg);
      redirect("admins/languages");
    }else{
      show_error('對不起,參數出錯');
      exit;
    }
  }
}